//
//  HJFriendAlertView.m
//  project
//
//  Created by jc on 2019/4/25.
//  Copyright © 2019 健健. All rights reserved.
//

#import "HJFriendAlertView.h"

@interface HJFriendAlertView ()
@property (nonatomic,strong) UIView * alertView;
@property (nonatomic,strong) UILabel * contenLabel;
@property (nonatomic,strong) UIButton * okBtn;
@property (nonatomic,strong) UIButton * cancelBtn;

@end


@implementation HJFriendAlertView

/**
 Description
 启动弹框
 @param view 内容视图
 @param animated 动画
 */
-(void)show:(UIView *)view animated:(BOOL)animated {
    self.alertView = view;
    view.frame = [UIScreen mainScreen].bounds;
    [[UIApplication sharedApplication].keyWindow addSubview:view];
    if (animated) {
        view.transform = CGAffineTransformScale(CGAffineTransformIdentity, CGFLOAT_MIN, CGFLOAT_MIN);
        [UIView animateWithDuration:0.3 animations:^{
            view.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.7, 1.7);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.2 animations:^{
                view.transform = CGAffineTransformIdentity;
            }];
        }];
    }
}

/**
 关闭弹框
 @param animated 动画
 */
-(void)dismissAnimated:(BOOL)animated {
    if (animated) {
        [UIView animateWithDuration:0.2 animations:^{
            self.alertView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 1.7, 1.7);
        } completion:^(BOOL finished) {
            [UIView animateWithDuration:0.3 animations:^{
                self.alertView.transform = CGAffineTransformScale(CGAffineTransformIdentity, 0.001, 0.001);
            } completion:^(BOOL finished) {
                [self.alertView removeFromSuperview];
            }];
        }];
    } else {
        [self.alertView removeFromSuperview];
    }
}



-(instancetype)init {
    if (self==[super init]) {
        [self configSubView];
    }
    return self;
}

-(void)configSubView {
    self.alertView = [UIView new];
    [self.alertView rounded:UIRectCornerAllCorners radius:10];
    [self addSubview:self.alertView];
    
    self.contenLabel = [UILabel new];
    self.contenLabel.textColor = [UIColor colorWithHex:0x333333];
    self.contenLabel.textAlignment = NSTextAlignmentCenter;
    self.contenLabel.font = [UIFont fontWithName:PingFangSC_Medium size:15];
    [self.alertView addSubview:self.contenLabel];
    
    self.okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.okBtn setTitle:@"确定" forState:0];
    self.okBtn.adjustsImageWhenHighlighted = NO;
    [self.okBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:0];
    [self.okBtn addTarget:self action:@selector(okBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:self.okBtn];
    
    
    self.cancelBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.cancelBtn setTitle:@"关闭" forState:0];
    self.cancelBtn.adjustsImageWhenHighlighted = NO;
    [self.cancelBtn setTitleColor:[UIColor colorWithHex:0x333333] forState:0];
    [self.cancelBtn addTarget:self action:@selector(cancelBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
    [self.alertView addSubview:self.cancelBtn];
    
}

-(void)layoutSubviews {
    [super layoutSubviews];
    @weakify(self)
    [self.alertView mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.size.mas_equalTo(CGSizeMake(120, 100));
        make.center.equalTo(self);
    }];
    
    [self.contenLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
       make.edges.equalTo(self.alertView).with.insets(UIEdgeInsetsMake(0, 12, 0, 12));
    }];
    
    [self.cancelBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.left.equalTo(self.alertView).offset(0);
        make.right.equalTo(self.okBtn).with.inset(0);
        make.height.equalTo(@30);
    }];
    
    [self.okBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        @strongify(self)
        make.left.equalTo(self.cancelBtn).offset(1);
        make.right.equalTo(self.alertView).with.inset(0);
        make.height.equalTo(@30);
    }];
}
/**
 确定
 @param btn 确定按钮
 */
-(void)okBtnClicked:(UIButton *)btn {
    
}

/**
 关闭视图
 @param btn 关闭按钮
 */
-(void)cancelBtnClicked:(UIButton *)btn {
    
}




@end
